package com.yuluke.ylsaudiorecorder;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.yuluke.ylsaudiorecorder.play.PlayActivity;
import com.yuluke.ylsaudiorecorder.play.PlayChooserActivity;
import com.yuluke.ylsaudiorecorder.record.RecordActivity;

public class EntranceActivity extends AppCompatActivity {

    private static final String TAG = "YlsRec_EntranceActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_entrance);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.button_goto_record).setOnClickListener( v -> {
            Intent intent = new Intent(this, RecordActivity.class);
            startActivity(intent);
        });
        findViewById(R.id.button_goto_play).setOnClickListener( v -> {
            Intent intent = new Intent(this, PlayChooserActivity.class);
            startActivity(intent);
        });

        requestRecordPermission();
    }

    private static final int PERMISSION_REQUEST_CODE_EXTERNAL_STORAGE = 201;
    private static final int PERMISSION_REQUEST_CODE_RECORD_AUDIO = 202;

    // TODO 梳理一下，如果用户没有允许权限怎么办？
    private void requestRecordPermission() {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "req ex storage permission");
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE_EXTERNAL_STORAGE);
        }
        if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "req record audio permission");
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
                    PERMISSION_REQUEST_CODE_RECORD_AUDIO);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i(TAG, "onRequestPermissionsResult, requestCode:" + requestCode
                + ", permissions size:" + permissions.length
                + ", grantResults size:" + grantResults.length);
        if (requestCode == PERMISSION_REQUEST_CODE_EXTERNAL_STORAGE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i(TAG, "req EXTERNAL_STORAGE permission:" + permissions[i]
                        + ", result:" +((grantResults[i] == PackageManager.PERMISSION_GRANTED)
                        ? "GRANTED" : "DENIED"));
            }
        } else if (requestCode == PERMISSION_REQUEST_CODE_RECORD_AUDIO) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i(TAG, "req RECORD_AUDIO permission:" + permissions[i]
                        + ", result:" +((grantResults[i] == PackageManager.PERMISSION_GRANTED)
                        ? "GRANTED" : "DENIED"));
            }
        }
    }
}